#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import logging
logging.basicConfig(level=logging.INFO)
import pdb
import sys
import  shutil 

'根据make命令中动态输入的SDK版本号，更新项目中的配置目录'
__author__='fengxw'

logging.info('argv start:')
logging.info(sys.argv)
target_sdk_path = sys.argv[1]
target_sdk_version = sys.argv[2]
logging.info('argv end:')

def copyfile(src , dst):
	logging.info('copyfile src:'+src+', dst:'+dst)
	shutil.copyfile(src ,dst);

def replace_in_file(old ,new , filepath):
	logging.info('replace_in_file old:'+old+', new:'+new+', filepath:'+filepath)
	file_read = open(filepath , 'r');
	lines_read = [];
	for line_read in file_read:
		line_read = line_read.replace(old ,new)
		lines_read.append(line_read)
	file_read.close();

	file_write = open(filepath , 'w');
	for line_write in lines_read:
		file_write.write(line_write);
	file_write.close();


env_files = [
['./mxtoolchain-dsv-arm-7g.cmake.template', './mxtoolchain-dsv-arm.cmake']
]

for item in env_files:
	copyfile(item[0] ,item[1])
	if(target_sdk_version in ( '20170918', '20171121' , '20171222' , '20180104' ) ):
		logging.info('this is 32 width sdk version')
		replace_in_file('{be_selected_sdk_path}' , target_sdk_path , item[1])
	else:
		logging.info('this is 64 width sdk version')
		replace_in_file('{be_selected_sdk_path}/sdk/sv-ds03h-system-sdk' , target_sdk_path+'/sdk/sv-g6s-system-sdk' ,item[1] )
		replace_in_file('SET(SYSROOTFS_HOME ${DSV_SDK_ROOT_PATH}/sysroots/cortexa7hf-neon-vfpv4-poky-linux-gnueabi)' ,
				'SET(SYSROOTFS_HOME ${DSV_SDK_ROOT_PATH}/sysroots/aarch64-poky-linux)',item[1] )




